<?php
namespace app\admin\model;

use think\Model;
use think\Db;
use think\facade\Config;
class ProductMiddleTable extends Model
{   
    protected $table='product_middle_table';

	/*查询数据 */
	/*查询数据 */
    public function get_data($page,$limit,$sear){
        $start=($page-1)*$limit+1;
        if(empty($sear)){
          $list=ProductMiddleTable::where([['status','=',1]])->order('create_time', 'desc')->limit($start,$limit)->select()->toArray();
        }else{
          $list=ProductMiddleTable::where([['status','=',1],['cname','like','%'.$sear.'%']])->order('create_time', 'desc')->limit($start,$limit)->select()->toArray();  
        }
        $data['count']=Classify::count();
        $data['list']=$list;
        return $data;
    }

    /*获取品牌，分类，材质信息*/
    public function get_brand_mater(){
        $brand=Brand::where([['status','=',1]])->field('bname,id')->order('create_time','desc')->select();
        $material=Material::where([['status','=',1]])->field('material,id')->order('create_time','desc')->select();
        $data['brand']=$brand;
        $data['material']=$material;
        return $data;  
    }
	/*获取一条数据用于修改*/
	public function get_edit($id){
        $data=Product::where([['status','=',1],['id','=',$id]])->find();
        $pext=Db::name('Product_ext')->where([['p_id','=',$id],['status','=',1]])->select();
        $class_model=new \app\admin\model\Classify();/*调用模型查询*/
        $class_model_data=$class_model->get_cascade_data($data['classify_id']);
        $data['classify']=$class_model_data;
        $data['pext']=$pext;
        return $data;  
	}
	/*新增*/
	public function insert_data($data){
        $Product = new Product;
        /*分类的判定*/
        if(!empty($data['quiz1']) ){
            if(!empty($data['quiz2']) ){
              if(!empty($data['quiz3']) ){
                if(!empty($data['quiz4'])){
                   $classify_id=$data['quiz4'];
                }else{
                   $classify_id=$data['quiz3']; 
                }
              }else{
                $classify_id=$data['quiz2']; 
              }
            }else{
              $classify_id=$data['quiz1'];  
            }
        }
       
      
		$result=$Product->save([
		    'Part_Number'  =>  trim($data['Part_Number']),
		    'classify_id' =>  trim($classify_id),
		    'brand_id' =>  trim($data['brand']),
            'material_id' =>  trim($data['material']),
		    'types' =>  trim($data['types']),
            'Price'  =>  trim($data['Price']),
            'Bearing_Type' =>  trim($data['Bearing_Type']),
            'For_Load_Direction' =>  trim($data['For_Load_Direction']),
            'Cross_Section' =>  trim($data['Cross_Section']),
            'Construction' =>  trim($data['Construction']),
            'Contact_Angle' =>  trim($data['Contact_Angle']),
            'Seal_Type'  =>  trim($data['Seal_Type']),
            'For_Shaft_Diameter'  =>  trim($data['For_Shaft_Diameter']),
            'Bore_Dia__d' =>  trim($data['Bore_Dia__d']),
            'Bore_Dia_Tolerance' =>  trim($data['Bore_Dia_Tolerance']),
            'For_Housing_ID' =>  trim($data['For_Housing_ID']),
            'Outer_Dia__D' =>  trim($data['Outer_Dia__D']),
            'Outer_Dia_Tolerance'  =>  trim($data['Outer_Dia_Tolerance']),
            'Width__B' =>  trim($data['Width__B']),
            'Width_Tolerance' =>  trim($data['Width_Tolerance']),
            'Washer_Material' =>  trim($data['Washer_Material']),
            'Ring_Material' =>  trim($data['Ring_Material']),
            'Ball_Material'  =>  trim($data['Ball_Material']),
            'Cage_Material' =>  trim($data['Cage_Material']),
            'Shield_Material' =>  trim($data['Shield_Material']),
            'Reference_Speed' =>  trim($data['Reference_Speed']),
            'Dynamic_Load_Rating__Cr' =>  trim($data['Dynamic_Load_Rating__Cr']),
            'Dynamic_Thrust' =>  trim($data['Dynamic_Thrust']),
            'Dynamic_Moment' =>  trim($data['Dynamic_Moment']),
            'Static_Load_Rating__C0r' =>  trim($data['Static_Load_Rating__C0r']),
            'Static_Thrust' =>  trim($data['Static_Thrust']),
            'Static_Moment' =>  trim($data['Static_Moment']),
            'Maximum_Speed__Grease'  =>  trim($data['Maximum_Speed__Grease']),
            'Maximum_Speed__Oil'  =>  trim($data['Maximum_Speed__Oil']),
            'Lubrication' =>  trim($data['Lubrication']),
            'Lubricant' =>  trim($data['Lubricant']),
            'Shaft_Mount_Type' =>  trim($data['Shaft_Mount_Type']),
            'Temperature_Range' =>  trim($data['Temperature_Range']),
            'Radial_Clearance_Trade_No'  =>  trim($data['Radial_Clearance_Trade_No']),
            'Radial_Clearance'  =>  trim($data['Radial_Clearance']),
            'ABEC_Rating'  =>  trim($data['ABEC_Rating']),
            'Ball_Qty'  =>  trim($data['Ball_Qty']),
            'Ball_Dia'  =>  trim($data['Ball_Dia']),
            'Rohs' =>  trim($data['Rohs']),
            'Weight' =>  trim($data['Weight']),
            'img' =>  trim($data['newname']),
            'dimg'  =>  trim($data['dimg']),
            'pfile'  =>  trim($data['pfile']),
		    'create_time' => date("Y-m-d H:i:s",time())
		]);
        $newId=$Product->id;
        /*设置默认seo属性*/
        $classname=Db::name('Classify')->where([['status','=',1],['id','=',$classify_id]])->field('cname')->find();
        $titleStr=trim($data['Part_Number'])."|". $classname['cname'];
        $keywords=trim($data['Part_Number']).",". $classname['cname'];
        $desc='';
        if(!empty($data['material'])){
           $materialname=Db::name('Material')->where([['status','=',1],['id','=',trim($data['material'])]])->field('material')->find();
           $titleStr.="|".$materialname['material'];
           $keywords.=",".$materialname['material'];
           $desc.=$materialname['material']." "; 
        }

        if(!empty($data['Bearing_Type'])){
           $titleStr.="|".$data['Bearing_Type'];
           $keywords.=",".$data['Bearing_Type'];
           $desc.=$data['Bearing_Type']." "; 
        }

        $seodata=['p_id'=>$newId,'title'=>$titleStr.Config::get('app.seo_title'),'keywords'=>$keywords.Config::get('app.seo_keywords'),'desc'=>Config::get('app.seo_desc').$desc.trim($data['Part_Number']),'create_time'=>date("Y-m-d H:i:s",time())];
        Db::name('Product_seo')->data($seodata)->insert();

		return $result;
	}
	/*修改*/
	public function update_data($id,$data){
        $change = Product::get($id);

        if(!empty($data['quiz1']) ){
            if(!empty($data['quiz2']) ){
              if(!empty($data['quiz3']) ){
                if(!empty($data['quiz4'])){
                   $classify_id=$data['quiz4'];
                }else{
                   $classify_id=$data['quiz3']; 
                }
              }else{
                $classify_id=$data['quiz2']; 
              }
            }else{
              $classify_id=$data['quiz1'];  
            }
        }
        $change->Part_Number = trim($data['Part_Number']);
        $change->classify_id = trim($classify_id);
        $change->brand_id  =  trim($data['brand']);
        $change->material_id =  trim($data['material']);
        $change->types =  trim($data['types']);
        $change->Price  =  trim($data['Price']);
        $change->Bearing_Type =  trim($data['Bearing_Type']);
        $change->For_Load_Direction =  trim($data['For_Load_Direction']);
        $change->Cross_Section =  trim($data['Cross_Section']);
        $change->Construction =  trim($data['Construction']);
        $change->Contact_Angle =  trim($data['Contact_Angle']);
        $change->Seal_Type  =  trim($data['Seal_Type']);
        $change->For_Shaft_Diameter  =  trim($data['For_Shaft_Diameter']);
        $change->Bore_Dia__d =  trim($data['Bore_Dia__d']);
        $change->Bore_Dia_Tolerance =  trim($data['Bore_Dia_Tolerance']);
        $change->For_Housing_ID =  trim($data['For_Housing_ID']);
        $change->Outer_Dia__D =  trim($data['Outer_Dia__D']);
        $change->Outer_Dia_Tolerance  = trim($data['Outer_Dia_Tolerance']);
        $change->Width__B =  trim($data['Width__B']);
        $change->Width_Tolerance =  trim($data['Width_Tolerance']);
        $change->Washer_Material =  trim($data['Washer_Material']);
        $change->Ring_Material =  trim($data['Ring_Material']);
        $change->Ball_Material  = trim($data['Ball_Material']);
        $change->Cage_Material = trim($data['Cage_Material']);
        $change->Shield_Material = trim($data['Shield_Material']);
        $change->Dynamic_Load_Rating__Cr =trim($data['Dynamic_Load_Rating__Cr']);
        $change->Dynamic_Thrust =trim($data['Dynamic_Thrust']);
        $change->Dynamic_Moment =trim($data['Dynamic_Moment']);
        $change->Static_Load_Rating__C0r = trim($data['Static_Load_Rating__C0r']);
        $change->Static_Thrust = trim($data['Static_Thrust']);
        $change->Static_Moment = trim($data['Static_Moment']);
        $change->Reference_Speed  =  trim($data['Reference_Speed']);
        $change->Maximum_Speed__Grease  =  trim($data['Maximum_Speed__Grease']);
        $change->Maximum_Speed__Oil  =  trim($data['Maximum_Speed__Oil']);
        $change->Lubrication =  trim($data['Lubrication']);
        $change->Lubricant =  trim($data['Lubricant']);
        $change->Shaft_Mount_Type =  trim($data['Shaft_Mount_Type']);
        $change->Temperature_Range = trim($data['Temperature_Range']);
        $change->ABEC_Rating  =  trim($data['ABEC_Rating']);
        $change->Radial_Clearance_Trade_No  =  trim($data['Radial_Clearance_Trade_No']);
        $change->Radial_Clearance  =  trim($data['Radial_Clearance']);
        $change->Rohs =  trim($data['Rohs']);
        $change->Ball_Dia =  trim($data['Ball_Dia']);
        $change->Ball_Qty =  trim($data['Ball_Qty']);
        $change->Weight = trim($data['Weight']);
        $change->img = trim($data['newname']);
        $change->dimg  = trim($data['dimg']);
        $change->pfile  = trim($data['pfile']);
        $result=$change->save();

		return $result;    
	}
    
	/*删除假动作实际为修改操作*/
	public function delete_data($id){
        $data = Product::get($id);
        if($data['status']!=1){
        	$result=['status'=>'0','msg'=>'无需操作'];
	        return json($result);
        }
		$data->status = '2';
		$result=$data->save();
		return $result;
	}

    /*设置seo*/
    public function setSeo($id,$title,$keywords,$desc){
        $data=['title'  => $title,'keywords' => $keywords,'desc' => $desc];
        $result=Db::name('product_seo')->where('p_id', $id)->update($data);
        return $result; 
    }

    /*获取id下的额外参数*/
    public function get_product_ext($id){
       $result=Db::name('product_ext')->where([['p_id','=',$id],['status','=',1]])->select();
       return $result;
    }

  
}
 